const app = getApp();
Page({
  data: {
    curGoods: {},
    curNum: 1,
    imgBaseUrl: app.globalData.imgBaseUrl,
    commnetValue: "",
    starNum: 5,
    salePrice: null,
    amount: null,
    goodsName: null,
    totalPrice: null,
    selected: false,
    hasList: true,
    userInfo: {},
    curIndex: 0,
    detail: [],

    isScroll: false,
    toView: 'shashi',
    starDesc: '非常满意，无可挑剔',
    stars: [{
      lightImg: '/images/star/star_light.png',
      blackImg: '/images/star/star_black.png',
      flag: 1,
      message: '非常不满意，各方面都很差'
    }, {
      lightImg: '/images/star/star_light.png',
      blackImg: '/images/star/star_black.png',
      flag: 1,
      message: '不满意，比较差'
    }, {
      lightImg: '/images/star/star_light.png',
      blackImg: '/images/star/star_black.png',
      flag: 1,
      message: '一般，还要改善'
    }, {
      lightImg: '/images/star/star_light.png',
      blackImg: '/images/star/star_black.png',
      flag: 1,
      message: '比较满意，仍要改善'
    }, {
      lightImg: '/images/star/star_light.png',
      blackImg: '/images/star/star_black.png',
      flag: 1,
      message: '非常满意，无可挑剔'
    }],
    assessLists: ['意见很有帮助', '态度非常好', '非常敬业', '非常专业认真', '回复很及时', '耐心细致']
  },
  /**
   * 快捷选择评论
   */
  switchComment(e) {
    console.log(e);
    this.setData({
      commentValue: e.currentTarget.dataset.item
    });
  },

  // 选择评价星星
  starClick: function (e) {
    var that = this;
    for (var i = 0; i < that.data.stars.length; i++) {
      var allItem = 'stars[' + i + '].flag';
      that.setData({
        [allItem]: 2
      })
    }
    var index = e.currentTarget.dataset.index;
    that.setData({
      starNum: index + 1,
    })
    console.log("star", e.currentTarget.dataset.index);
    for (var i = 0; i <= index; i++) {
      var item = 'stars[' + i + '].flag';
      that.setData({
        [item]: 1
      })
    }
    this.setData({
      starDesc: this.data.stars[index].message
    })
  },

  /**
   * 提交评价
   */
  submitEvaluation: function (e) {
    console.log(e);
    let that = this;
    wx.request({
      url: app.globalData.baseUrl + "/comments",
      header: {
        "cookie": "JESSIONID=" + that.data.userInfo.sessionId
      },
      method: "POST",
      data: {
        delivery: that.data.starNum,
        content: e.detail.value.commentValue,
        userId: that.data.userInfo.userId
      },
      success(res) {
        if (res.statusCode === 201) {
          wx.showToast({
            title: '感谢您的评价！',
            icon: 'success',
            duration: 1500
          });
        } else {
          wx.showToast({
            title: '服务器异常！',
            icon: 'loading',
            duration: 1500
          });
        }
        console.log("commentRes", res);
      },
      fail(err) {
        wx.showToast({
          title: '服务器异常！',
          icon: 'loading',
          duration: 1500
        });
        console.log("commentErr", err);
      }
    })
    wx.showToast({
      title: '感谢您的评价！',
      icon: "success",
      duration: 1500
    })
  },
  onLoad: function (event) {
    let that = this;
    that.setData({
      userInfo: wx.getStorageSync("userInfo")
    });
  }
})